﻿using Microsoft.Extensions.Configuration;
using System.Net.Mail;
using System.Net.Mime;

var configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddUserSecrets<Program>()
    .Build();

var mailMsg = new MailMessage();
mailMsg.From = new MailAddress(configuration.GetSection("Email:From").Value);
mailMsg.To.Add(new MailAddress(configuration.GetSection("Email:To").Value));
mailMsg.Subject = "Email Test Demo(Smtp)";
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("Example Plain Text Message Body", null, MediaTypeNames.Text.Plain));

// 邮件推送的SMTP地址和端口
var smtpClient = new SmtpClient("smtpdm.aliyun.com", 25);
//C#官方文档介绍说明不支持隐式TLS方式，即465端口，需要使用25或者80端口(ECS不支持25端口)，另外需增加一行 smtpClient.EnableSsl = true; 故使用SMTP加密方式需要修改如下：
//SmtpClient smtpClient = new SmtpClient("smtpdm.aliyun.com", 80);
//smtpClient.EnableSsl = true;

// 使用SMTP用户名和密码进行验证
smtpClient.Credentials = new System.Net.NetworkCredential(configuration.GetSection("Email:UserName").Value, configuration.GetSection("Email:Password").Value);
smtpClient.Send(mailMsg);